<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>

<head>
	<style type="text/css">
	h1 span{visibility:visible;}
	td span{visibility:visible;}
	.msg{float:left; line-height:1em; margin:0; display:block;border:1px solid #f66;padding:2px 5px;*padding:2px 5px 1px;}
	.msg.rn{background-color:#fff2f2;border-color:#f66;}
	</style>
</head>

<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath() %>'/>
<c-rt:set var="frontHostname" value='<%=ServerValue.getFrontHostname() %>'/>
<s:url value="/images" var="imgPath"/>
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<!-- 時間 -->	
<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
<link rel="stylesheet" type="text/css" href="/js/timePicker.css"/>
<script type="text/javascript" charset="UTF-8" src="/js/jquery-ui-1.7.2.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/jquery-ui-1.7.2.custom.css"/>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css" />
<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>

<script>
$().ready(function() {
	$('#startDate').datepick({minDate: null, showOn: 'both', 
    	buttonImageOnly: true, buttonImage: '${imgPath}/calendar.gif',numberOfMonths:2,dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	$('#endDate').datepick({minDate:new Date(), showOn: 'both', 
    	buttonImageOnly: true, buttonImage: '${imgPath}/calendar.gif',numberOfMonths:2,beforeShow:customRange,dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
	$("#startTime").timePicker();
	$("#endTime").timePicker();
	
	$('input[id^=creditcard_range_]').click(function() {
		var range = this.id.replace('creditcard_range_', '');
		if(range == 'Order') {
			$('#RangeTR').hide();
		} else {
			$('#RangeTR').show();
		}
	});
	
	if( $('#creditcard_range_Order').attr('checked') ) {
		$('#RangeTR').hide();
	} else {
		$('#RangeTR').show();
	}
	
	$('#detail_add').click(function() {
		cleanTip();
		
		var newBankId = $('#bankId').val();
		var BankIdArr = $('#bankIdNote').val().split(',');
		var isExist = false;
		for(var i = 0; i < BankIdArr.length; i++) {
			if( BankIdArr[i] == newBankId ) {
				isExist = true;
				break;
			}
		}
		if( !isExist ) {
			$('#campaignForm').attr('action', 'addDetail.do');
			$('#campaignForm').submit();
		} else {
			appendTip('bankId', '此银行及卡别已經设定', true);
		}
	});
	
	$('a[id^=detail_del_]').click(function() {
		var operation = this.id.replace('detail_del_', '');
		$('#operation').val(operation);
		$('#campaignForm').attr('action', 'delDetail.do');
		$('#campaignForm').submit();
	});
	
	$('input[id^=tmp_periods_]').click(function() {
		var no = this.id.replace('tmp_periods_', '');
		if(this.checked) {
			$('#periods_' + no).val(this.value);
			if( $('#fees_' + no).val() == '' )
				$('#fees_' + no).val( $('#ori_fees_' + no).val() );
			if( $('#downLimits_' + no).val() == '' )
				$('#downLimits_' + no).val( $('#ori_downLimits_' + no).val() );
			if( $('#upLimits_' + no).val() == '' )
				$('#upLimits_' + no).val( $('#ori_upLimits_' + no).val() );
		} else { 
			$('#periods_' + no).val('');
			$('#fees_' + no).val('');
			$('#downLimits_' + no).val('');
			$('#upLimits_' + no).val('');
		}
	});
	
	$('#sbtn').click(function() {
		cleanTip();
		if( validate_data() ) {
			$('#campaignForm').submit();
		}
	});
	
	$('input[id^=fees_]').ydPrice();
	$('input[id^=downLimits_]').ydPrice();
	$('input[id^=upLimits_]').ydPrice();
});

function validate_data() {
	var isCorrect = true; <%-- 预设验证正确 --%>
	
	cleanTip();
	
	for(var i = 0; i < $('input[id^=bankId_]').length; i++) {
		var isPeriodBlank = true;
		var bankName = $('#bankName_' + i).val();
		var tp = 'periods_' + i;
		for(var j = 0; j < $('input[id^='+tp+']').length; j++) {
			var no = i + '' + j;
			if( $('#tmp_periods_' + no).attr('checked') && $('#periods_' + no).val().length == 0 ) {
				$('#periods_' + no).val( $('#tmp_periods_' + no).val() );
			} else if( !($('#tmp_periods_' + no).attr('checked')) && $('#periods_' + no).val().length > 0 ) {
				$('#periods_' + no).val( '' );
			}
			
			if( $('#periods_' + no).val().length > 0 ) {
				isPeriodBlank = false;
				if( $('#fees_' + no).val().length == 0 ) {
					appendTip('detail', '请输入'+bankName+'-'+$('#tmp_periods_' + no).val()+'期的会员自付手续费', false);
					isCorrect = false;
				} else if( parseFloat($('#fees_' + no).val()) < parseFloat($('#ori_fees_' + no).val()) ) {
					var ans = confirm(bankName+'会员自付手续费('+$('#fees_' + no).val()+'%)低于基本设置('+$('#ori_fees_' + no).val()+'%), \n'
							+ '确定要这样设定? ');
					if( !ans ) {
						$('#fees_' + no).focus();
						return false;
					}
				}
				
				if( $('#downLimits_' + no).val().length == 0 || $('#upLimits_' + no).val().length == 0 ) {
					appendTip('detail', '请输入'+bankName+'-'+$('#tmp_periods_' + no).val()+'期的交易金额上下限', false);
					isCorrect = false;
				} else if( parseFloat($('#downLimits_' + no).val()) > parseFloat($('#upLimits_' + no).val()) ) {
					appendTip('detail', bankName+'-'+$('#tmp_periods_' + no).val()+'期交易下限金额要小于上限金额', false);
					isCorrect = false;
				} else if( parseFloat($('#downLimits_' + no).val()) < parseFloat($('#ori_downLimits_' + no).val()) ) {
					var ans = confirm(bankName+'交易金额下限(￥'+$('#downLimits_' + no).val()+'元)低于基本设置(￥'+$('#ori_downLimits_' + no).val()+'元), \n'
							+ '确定要这样设定? ');
					if( !ans ) {
						$('#downLimits_' + no).focus();
						return false;
					}
				} else if( parseFloat($('#upLimits_' + no).val()) > parseFloat($('#ori_upLimits_' + no).val()) ) {
					var ans = confirm(bankName+'交易金额上限(￥'+$('#upLimits_' + no).val()+'元)低于基本设置(￥'+$('#ori_upLimits_' + no).val()+'元), \n'
							+ '确定要这样设定? ');
					if( !ans ) {
						$('#upLimits_' + no).focus();
						return false;
					}
				}
			} else {
				if( $('#fees_' + no).val().length > 0 
						|| $('#downLimits_' + no).val().length > 0
						|| $('#upLimits_' + no).val().length > 0 ) {
					appendTip('detail', '请清除'+bankName+'-'+$('#tmp_periods_' + no).val()+'期的手续费、交易金额上下限，或选择'+$('#tmp_periods_' + no).val()+'期', false);
					isCorrect = false;
				}
			}
		}
		
		if( isPeriodBlank ) {
			appendTip('detail', '请选择'+bankName+'信用卡支持期数', false);
			isCorrect = false;
		}
	}
	
	if( $('#creditcard_memo').val() == '' ) {
		appendTip('creditcard_memo', '请输入信用卡活动名称', true);
		isCorrect = false;
	}
	if( $('#campaignForm_ignoreCookiefalse').attr('checked') ) {
		if( $('#creditcard_cookieArray').val() == null ) {
			appendTip('creditcard_cookieArray', '请选择特定活动渠道 ', true);
			isCorrect = false;
		}
	}
	if($('#startDate').val()>$('#endDate').val()) {
		appendTip('endDate', '结束日期不能小于起始日期', true);
		isCorrect = false;
	} else if($('#startDate').val()==$('#endDate').val()) {
		if($('#startTime').val()>=$('#endTime').val()){
			appendTip('endDate', '结束时间需大于起始时间', true);
			isCorrect = false;
		}
	}
	if( $('#endDate').val() == '' ) {
		appendTip('endDate', '请输入结束时间', true);
		isCorrect = false;
	}
	if( $('#startDate').val() == '') {
		appendTip('startDate', '请输入起始时间', true);
		isCorrect = false;
	}
	if( $('#creditcard_campaignDetail').val() == '' ) {
		appendTip('creditcard_campaignDetail', '请输入信用卡活动规则', true);
		isCorrect = false;
	}
	if( $('#creditcard_campaignDesc').val() == '' ) {
		appendTip('creditcard_campaignDesc', '请输入信用卡活动说明', true);
		isCorrect = false;
	}
	if( $('#creditcard_campaignName').val() == '' ) {
		appendTip('creditcard_campaignName', '请输入信用卡活动名称', true);
		isCorrect = false;
	}
	
	return isCorrect;
}

function customRange (){
	var startDt = $('#startDate').val(); //起始日
	var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
	return {minDate:endDt}; 
}

<%-- 清空提示 --%>
function cleanTip() {
	$("span:[id^=Tip_]").hide();
	$("span:[id^=Tip_]").text('');
}
<%-- 附加提示 --%>
function appendTip(id, msg, isFocus) {
	if( !(isFocus == true || isFocus == false) ) 
		isFocus = true;
	
	var target = $('#Tip_' + id);
	if(target.text().length > 0) {
		appendWord(target, msg);
	} else
		target.text(msg);
	target.show();
	if(isFocus)
		$('#' + id).focus();
}

function appendWord(origin, apptext){
	if(origin.text().length==0){
		origin.text(apptext);
	}else{
		origin.text(origin.text()+', '+apptext);
	}
}
</script>

<s:if test="hasErrors()">
<p><s:actionerror cssClass="msg rn" /></p>
</s:if>
<s:if test="hasActionMessages()">
<p><s:actionmessage cssClass="msg rn" /></p>
</s:if>
<s:if test="hasFieldErrors()">
<p><s:fielderror cssClass="msg rn" /></p>
</s:if>

<h1>信用卡活动管理</h1>
<s:form action="campaign/save.do" namespace="/creditcard" method="post" id="campaignForm" name="campaignForm" >
<s:hidden id="operation" name="operation" />
<s:hidden name="creditcard.id" />
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="creditcard">
	<tr>
		<th><span class="txt-rd">＊</span>1.活动名称：</th>
		<td>
			<s:textfield id="creditcard_campaignName" name="creditcard.campaignName" cssClass="w-600" />
			<span id="Tip_creditcard_campaignName" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>2.活动说明：</th>
		<td>
			<s:textarea id="creditcard_campaignDesc" name="creditcard.campaignDesc" cssClass="w-600" rows="5"></s:textarea>
			<span id="Tip_creditcard_campaignDesc" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>3.活动规则：</th>
		<td>
			<s:textarea id="creditcard_campaignDetail" name="creditcard.campaignDetail" cssClass="w-600" rows="5"></s:textarea>
			<span id="Tip_creditcard_campaignDetail" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>4.活动类型：</th>
		<td>
			<s:radio list="menuRange" id="creditcard_range_" name="creditcard.range" listValue="desc" ></s:radio>
		</td>
	</tr>
	<tr id="RangeTR">
		<th><span class="txt-rd">＊</span>5.适用范围设定：</th>
		<td>
			<p>
				<s:textarea id="creditcard_campaignRangeDet" name="creditcard.campaignRangeDet" cssClass="w-600" rows="5"></s:textarea>
				<span id="Tip_creditcard_campaignRangeDet" class="msg rn" style="display:none;"></span>
			</p>
			<p>          (请以半角,字元分隔, 当适用范围为商品时, 请填入商品编号; 当适用范围为分类时, 请填入分类编号)</p>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>6.活动期间：</th>
		<td>
			<input name="startDate" id="startDate" size="18" value="<fmt:formatDate value="${creditcard.startDate}" pattern="yyyy-MM-dd"/>" >
			<input name="startTime" id="startTime" size="10" value="<fmt:formatDate value="${creditcard.startDate}" pattern="HH:mm"/>"> ~ 
			<input name="endDate" id="endDate" size="18" value="<fmt:formatDate value="${creditcard.endDate}" pattern="yyyy-MM-dd"/>" >
			<input name="endTime" id="endTime" size="10" value="<fmt:formatDate value="${creditcard.endDate}" pattern="HH:mm"/>">
			<span id="Tip_startDate" class="msg rn" style="display:none;"></span>
			<span id="Tip_endDate" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>7.活动渠道：</th>
		<td>
			<p>
				<label>
					<s:radio list="#{'true':'全部渠道'}" name="ignoreCookie" listKey="key" listValue="value"/>
				</label>
			</p>
			<p>
				<label>
					<s:radio list="#{'false':'特定渠道'}" name="ignoreCookie" listKey="key" listValue="value"/>
				</label>
				<br />
				<s:select id="creditcard_cookieArray" name="creditcard.cookieArray" multiple="true" size="5" list="menuChannel.options" listKey="key" listValue="value.codeName" cssClass="w-600" />
				<span id="Tip_creditcard_cookieArray" class="msg rn" style="display:none;"></span>
			</p>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>8.活动Domain：</th>
		<td>
			<s:select name="creditcard.domain" list="menuDomain.options" listKey="key" listValue="value.name"/>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>9.是否启用：</th>
		<td>
			<s:radio list="#{'true':'是','false':'否'}" name="creditcard.enable" listKey="key" listValue="value"></s:radio>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>10.备注：</th>
		<td>
			<s:textarea id="creditcard_memo" name="creditcard.memo" rows="3" cssClass="w-600"/>
			<span id="Tip_creditcard_memo" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
	<tr>
		<th>&nbsp;&nbsp;&nbsp; 11.修改记录：</th>
		<td>
			<p>建立：<fmt:formatDate value="${creditcard.createDate}" pattern="yyyy-MM-dd HH:mm"/> / ${creditcard.createUser}</p>
			<p>修改：<fmt:formatDate value="${creditcard.modifyDate}" pattern="yyyy-MM-dd HH:mm"/> / ${creditcard.modifyUser}</p>
		</td>
	</tr>
	<tr>
		<th><span class="txt-rd">＊</span>12.银行及卡别设定：</th>
		<td>
			<p>
				<label><a id="did"></a>
					<s:select id="bankId" name="bankId" list="menuSetting" listKey="key" listValue="value.creditcardName" />
					<s:hidden name="bankIdNote" id="bankIdNote" />
				</label>
				<span class="l-link"><a id="detail_add" href="#did">新增</a></span>
				<br/><span id="Tip_bankId" class="msg rn" style="display:none;"></span>
			</p>
			<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tb-05">
				<thead>
					<tr>
						<th>银行别</th>
						<th><span class="creditcard-dtl-1">支持期数</span><span class="creditcard-dtl-2">会员自付手续费(%)</span><span class="creditcard-dtl-3">交易金额上下限</span></th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<s:iterator value="showDetails" var="detail" status="idx" >
						<s:set name="i" value="%{#idx.index}" />
						<s:set name="bank" value="%{menuSetting[#detail.bankId]}" />
					<tr>
						<td>
							${detail.bankName}
							<input type="hidden" name="showDetails[${i}].id" value="${detail.id}" />
							<input type="hidden" name="showDetails[${i}].bankId" value="${detail.bankId}" id="bankId_${i}" />
							<input type="hidden" name="showDetails[${i}].bankName" value="${detail.bankName}" id="bankName_${i}" />
						</td>
						<td align="center">
							<s:iterator value="#bank.periods" var="period" status="idx2" >
								<s:set name="i2" value="%{#idx2.index}" />
							<p>
								<label class="creditcard-dtl-1">
									<input type="checkbox" id="tmp_periods_${i}${i2}" value="${period}" ${detail.periodMap[period]==period?'checked':''} /> ${period}
									<input id="periods_${i}${i2}" name="showDetails[${i}].periods[${i2}]" type="hidden" value="${detail.periodMap[period]}" >
									<input type="hidden" id="ori_periods_${i}${i2}" value="${period}" />
								</label>
								<label class="creditcard-dtl-2">
									<input id="fees_${i}${i2}" name="showDetails[${i}].fees[${i2}]" type="text" value="${detail.feeMap[period]}" size="2" />
									<input type="hidden" id="ori_fees_${i}${i2}" value="${bank.feeMap[period]}" />
								</label>
								<label class="creditcard-dtl-3">
									<input id="downLimits_${i}${i2}" name="showDetails[${i}].downLimits[${i2}]" type="text" value="${detail.downLimitMap[period]}" size="5" />
									<input type="hidden" id="ori_downLimits_${i}${i2}" value="${bank.downLimitMap[period]}" />
									~
									<input id="upLimits_${i}${i2}" name="showDetails[${i}].upLimits[${i2}]" type="text" value="${detail.upLimitMap[period]}" size="5" />
									<input type="hidden" id="ori_upLimits_${i}${i2}" value="${bank.upLimitMap[period]}" />
								</label>
							</p>
							</s:iterator>
						</td>
						<td align="center"><a id="detail_del_${i}" href="#" class="a-del">删除</a></td>
					</tr>
					</s:iterator>
				</tbody>
			</table>
			<span id="Tip_detail" class="msg rn" style="display:none;"></span>
		</td>
	</tr>
</table>
</s:form>
<p class="f-link"><a id="sbtn" href="#">储存</a> <a href="search.do">回查询列表</a></p>